define(function(require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('../../su/xsySuInfo/widget');
    require('core/widgets/map/amap');
    require('core/widgets/itemsCRU/item');

    var oTpl = $.parseTpl(require('platform/widgets/crm/account/xsyAccountInfo/widget.tpl'));
    $.widget('rk.xsyAccountInfo', $.rk.xsySuInfo, {
        _params: {},
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            me._beIniting();
            me._loadData({}, function(result) {
                // result.uuid //控件的uuid
                // result.common //页面公共的数据
                // result.self//控件自己的数据
                elem.html(oTpl.content);
                me.detail = elem.find('.account-detail');
                me.detail.itemsinplace({
                    // elem.itemsform({
                    entities: result.self.entityData,
                    items: result.self.itemList,
                    savesuccess: function(evt, arg) {
                        // arg.element: 触发编辑的DOM元素
                        // arg.item: 编辑的字段
                        // arg.newValue: 保存成功的值
                        // arg.entity: 组件当前的全部数据
                        try {
                            //广播一个【外部】事件，控件以外会监听到
                            me._triggerOuterEvent('detailChanged', arg);
                        } catch (ex) {
                            console.log('trigger detailChanged error: ', ex);
                        }
                    },
                    savefail: function(evt, arg) {
                        if (arg.response.status == '300003') {
                            $.msg(arg.item.label + '重复');
                        }
                    }
                });

                me.amap = $(oTpl.map).appendTo(me.detail).find('.account-map').uniqueId();

                var entityData = result.self.entityData,
                    mapInfo = entityData.longitude && entityData.latitude || entityData.location ? {
                        address: entityData.location,
                        longitude: entityData.longitude,
                        latitude: entityData.latitude
                    } : null;
                if (mapInfo) {
                    me.amap.closest('div.xsy_su_info').show();
                    me.amap.amap({
                        zoomEnable: false,
                        divName: me.amap.attr('id')
                    }, mapInfo);
                }

                me.wideview = me.detail.find('a.account-map-wideview');
                me.wideview.on('click', function(evt) {
                    var dialog = $('<div class="pop_up_02"><div class="pop_up_content"><div class="add_new_list clear js-contentDiv" style="width:700px;height:430px;" id="newMapId" ></div></div><div class="pop_up_bottom"><div class="pop_up_button"><a  title="" href="javascript:;" class="green_button js-btnConfirm" act="close">关闭</a></div></div></div>');
                    dialog.popup({
                        width: 800,
                        height: 600,
                        title: '查看',
                        resize: true,
                        create: function() {
                            dialog.find('div.js-contentDiv').amap({
                                zoomEnable: true,
                                divName: 'newMapId',
                                scale: true,
                                toolBar: true
                            }, mapInfo);
                        }
                    });
                });

                // 收听【外部】控件广播而来的事件
                me._listenOuterEvent('afterPopupEdit', function(arg) {
                    me._loadData({}, function(result) {
                        var itemList = result.self.itemList;
                        var entityData = result.self.entityData;
                        itemList.forEach(function(item){
                            entityData[item.entryPropertyNameOnly] != 0 &&  (!entityData[item.entryPropertyNameOnly]) && (entityData[item.entryPropertyNameOnly] = null);
                        })
                        me.detail.itemsinplace('update', result.self.entityData);
                    });
                });
            }, function() {
                console.log('loading data error');
            });
        },

        reloadItem: function(itemList) {
            var me = this;
            var elem = me.element;
            var opt = me.options;


            elem.html(require('platform/widgets/crm/account/xsyAccountInfo/widget.tpl'));
            me.detail = elem.find('.account-detail');
            me.amap = elem.find('.account-map').uniqueId();
            me.wideview = elem.find('a.account-map-wideview');
            me.detail.itemsinplace({
                // elem.itemsform({
                entities: {},
                items: itemList,
            });

        },

        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        }
    });
});
